package cn.edu.nuc.crm.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import cn.edu.nuc.crm.dto.PageInfo;
import cn.edu.nuc.crm.entity.Account;
import cn.edu.nuc.crm.entity.Admin;
import cn.edu.nuc.crm.entity.Manager;
import cn.edu.nuc.crm.entity.Product;
import cn.edu.nuc.crm.entity.Role;
import cn.edu.nuc.crm.entity.Sysfunction;
import cn.edu.nuc.crm.service.interfaces.AccountService;
import cn.edu.nuc.crm.service.interfaces.AdminService;
import cn.edu.nuc.crm.service.interfaces.ManagerService;
import cn.edu.nuc.crm.service.interfaces.ProductService;
import cn.edu.nuc.crm.service.interfaces.RoleService;
import cn.edu.nuc.crm.service.interfaces.SysfunctionService;

@Controller
public class SysfunctionController {
	
	@Autowired
	private AccountService accountService;
	@Autowired
	private AdminService adminService;
	@Autowired
	private ManagerService managerService;
	@Autowired
	private SysfunctionService sysfunctionService;
	@Autowired
	private RoleService roleService;
	@Autowired
	private ProductService productService;
	
	/**
	 * 注销
	 * @param session
	 * @return
	 */
	@RequestMapping(value="/logout")
	public String logout(HttpSession session){
		session.removeAttribute("account");
		return "login1";
		
	}
	/**
	 * 首页
	 * @param req
	 * @return
	 */
	
	@RequestMapping(value="/page",method=RequestMethod.GET)
	public String page(HttpServletRequest req){
		
		List<Product> list= productService.product();
		req.setAttribute("pi1", list);
		return "page";
	}
	
	
	/**
	 * 管理员，客户，经理不同的登录方式
	 * @return
	 */
	@RequestMapping(value="/login",method=RequestMethod.GET)
	public String login(){
		
		return "login1";
	}
	@RequestMapping(value="/login")
	public String login(Admin admin,Manager manager,Account account,HttpServletRequest req,HttpServletResponse resp,HttpSession session){
		if(req.getParameter("choose").equals("admin")){
			try {
				Admin ad = adminService.login(req.getParameter("name"),req.getParameter("password"));
				
				int roleid=ad.getRoleid();
				Role role=roleService.choose(roleid);
				String name1=ad.getAdminnane();
				String name2=role.getRolename();
				req.setAttribute("name1", name1);
				req.setAttribute("name2", name2);
				session.setAttribute("ad", ad);
				List<Sysfunction> list=sysfunctionService.select();   //select是管理员
				req.setAttribute("list", list);
				return "index";
			} catch (Exception e) {
				e.printStackTrace();
			}
			req.setAttribute("msg", "登录失败");
			return "login1";
	   }
		else if(req.getParameter("choose").equals("manager"))
		{
			try{
				manager.setManagername(req.getParameter("name"));
				manager.setManagerpassword(req.getParameter("password"));
				Manager man=managerService.login(manager.getManagername(),manager.getManagerpassword());
				req.setAttribute("manager",man);
				session.setAttribute("man", man);
				int i=man.getRoleid();
				Role role=roleService.choose(i);
				String name1=man.getManagername();
				String name2=role.getRolename();
				req.setAttribute("name1", name1);
				req.setAttribute("name2", name2);
				
				
				List<Sysfunction> list=sysfunctionService.select2();   //select2是经理
				req.setAttribute("list", list);
				return "index";
			}catch(Exception e){
				e.printStackTrace();
			}
			return "login1";
		}else{
			try {
				account.setAccountname(req.getParameter("name"));
				account.setAccountpassword(req.getParameter("password"));
				Account ac=accountService.login(account.getAccountname(), account.getAccountpassword());
				//req.setAttribute("account", ac);
				int roleid=ac.getRoleid();
				Role role=roleService.choose(roleid);
				String name1=ac.getAccountname();
				String name2=role.getRolename();
				req.setAttribute("name1", name1);
				req.setAttribute("name2", name2);
				session.setAttribute("account", ac);
				req.setAttribute("account", ac);
//				List<Product> list= productService.product();
//				req.setAttribute("pi1", list);
				List<Sysfunction> list=sysfunctionService.select3();  //select3是客户
				req.setAttribute("list", list);
				return "index";
			} catch (Exception e) {
				e.printStackTrace();
			}
			req.setAttribute("msg", "用户名或密码错误");
			return "login1";
	    }
	}
	
	/*@RequestMapping(value="/adminlist")
	public String sysfunctionlist(HttpSession session){
		List<Sysfunction> sysfunctionlist=new ArrayList<Sysfunction>();
		sysfunctionlist=sysfunctionService.sysfunctionlist();
		System.out.println(sysfunctionlist);
		session.setAttribute("sysfunction",sysfunctionlist);
		return "/adminlist";	
	}*/
	/**
	 * 列出功能列表带分页显示
	 * @param current
	 * @param session
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/adminlist",method=RequestMethod.GET)
	public String select(@RequestParam(name="current",defaultValue="1") int current,
			HttpSession session, Model model){	
		try {
			List<Sysfunction> sysfunctionlist = new ArrayList<Sysfunction>();
			sysfunctionlist = sysfunctionService.sysfunctionlist();
			session.setAttribute("sysfunction", sysfunctionlist);
			System.out.println(sysfunctionlist);
			PageInfo pi = sysfunctionService.sysfunction(current);
			System.out.println(pi);
			session.setAttribute("pi", pi);
			
			return "function/adminlist";
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "redirect:login1";
	}
	/**
	 * 添加功能
	 * @return
	 */
	@RequestMapping(value="/function/add",method=RequestMethod.GET)
	public String add(){	
		return "/function/add";
	}
	/**
	 * 修改功能
	 * @param funpid
	 * @param funpname
	 * @param req
	 * @return
	 */
	@RequestMapping(value="/edit",method=RequestMethod.GET)
	public String edit(int funpid,String funpname,HttpServletRequest req){	
		req.setAttribute("funpid", funpid);
		req.setAttribute("funpname", funpname);	
		return "/function/add";
	}
	@RequestMapping(value="funadd",method=RequestMethod.POST)
	public String add(Sysfunction sysfunction){
		if(sysfunction.getFunpid()==null){
			sysfunction.setFunpid(-1);
			int count = sysfunctionService.insert(sysfunction);
			if( count < 0){
				return "/function/add";
			}
		}else{
			int count = sysfunctionService.insert(sysfunction);
			if( count < 0){
				return "/function/add";
			}
		}	
		return "redirect:adminlist";
	}
	/**
	 * 模糊查询
	 * 根据功能名称查找功能
	 * @return
	 */
	@RequestMapping(value="funsearch",method=RequestMethod.POST)
	public String selectByName(Model model,String funname){
		System.out.println(funname);
		if( funname != null && !funname.equals("") ){
			List<Sysfunction> list = sysfunctionService.selectByName(funname);
			model.addAttribute("list", list);
			return "/function/search";
		}
		return "redirect:adminlist";	
	}	
	/**
	 * 删除功能
	 * @param funid
	 * @return
	 */
	@RequestMapping(value="/fundelete",method=RequestMethod.GET)
	public String delete(Integer funid,HttpSession session){
		int result=sysfunctionService.deleteByPrimaryKey(funid);
		if(result>0){	
			return "redirect:/adminlist";	
		}else{
			session.setAttribute("info", "该功能无法删除");
			return "/adminlist";
		}
	}
	
	/**
	 * 编辑功能
	 * @param funid
	 * @param session
	 * @return
	 */
	@RequestMapping(value="tofunedit",method=RequestMethod.GET)
	public String updatefun(int funid,HttpSession session){
		Sysfunction function=new Sysfunction();
		function=sysfunctionService.selectByPrimaryKey(funid);
		System.out.println(function.getFunid());
		session.setAttribute("item",function) ;
		return "function/edit";	
	}
	@RequestMapping(value="funedit",method=RequestMethod.POST)
	public String updatefun(HttpServletRequest request,Sysfunction sys){
		try{
		sysfunctionService.updateByPrimaryKeySelective(sys);
		return "redirect:adminlist";
		}catch(Exception e){
			request.setAttribute("msg", "修改失败");
			return "function/edit";
		}	
	}
	@RequestMapping(value="/loginAccount",method=RequestMethod.POST)
	public String registerAccount(String accountname,String accountpassword,String accountphone,HttpServletRequest req,HttpServletResponse resp,HttpSession session){
		try{
			
			Account ac=accountService.login(accountname,accountpassword);
			//req.setAttribute("account", ac);
			int roleid=ac.getRoleid();
			Role role=roleService.choose(roleid);
			String name1=ac.getAccountname();
			String name2=role.getRolename();
			req.setAttribute("name1", name1);
			req.setAttribute("name2", name2);
			session.setAttribute("account", ac);
			req.setAttribute("account", ac);
//			List<Product> list= productService.product();
//			req.setAttribute("pi1", list);
			List<Sysfunction> list=sysfunctionService.select3();  //select3是客户
			req.setAttribute("list", list);
			return "index";
		}
		catch(Exception e){
			e.printStackTrace();
	   }	
		return "/login";	
	}
	
	//访问车贷网官网登陆
		@RequestMapping(value="/gologinindex",method=RequestMethod.GET)
		public String gologinindex(){						
						return "index";
		}
	
	
}
